package controllers;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import models.LeaMes;
import models.Manager;
import models.Member;
import play.mvc.Controller;
import play.mvc.With;
import service.LeaMesService;

@With(Secure.class)
public class LeaMesAction extends Controller {
	
	/**
	 * 全查表
	 */
	@Check("ResourceManage")
	public static void list(Integer page, Integer size){
		page = null != page ? page : 1;
		size = null != size ? size : 10;
		int offset = (page - 1) * size;
		
		List leaList = LeaMes.find("").from(offset).fetch(size);
		Long count = LeaMes.count();
		Long pages = (count+size-1)/size;
		render(leaList, count, size, page, pages, offset);
	}
	
	/**
	 * 留言查看，管理员回复
	 * @param id 留言编号
	 */
	@Check("ResourceManage")
	public static void show(long id){
		LeaMes lea = LeaMesService.findById(id);
		String mes = null;
		if(lea.lerDet == null || "".equals(lea.lerDet) ){  //未回复的
			render("LeaMesAction/show.html",lea,mes);
		}else{
			mes = "已回复";
			render("LeaMesAction/show.html",lea,mes);
		}
	}
	
	/**
	 * 留言回复
	 * @param lerDet 
	 * @param id
	 */
	@Check("ResourceManage")
	public static void saveLea(String lerDet,long id){
		if(lerDet == null || "".equals(lerDet)){
			flash.success("留言回复失败 ！");
		}else{
			LeaMes lea = LeaMesService.findById(id);
			lea.managers = Manager.findById(Long.parseLong(session.get("aid").toString())); 	//拿到当前登陆进行留言的管理员id
			lea.lerTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(
						  new Date(System.currentTimeMillis())).toString();
			lea.lerDet = lerDet;
			LeaMesService.save(lea);
			flash.success("留言回复成功 ！");
		}
		list(1,10);
	}
	
	@Check("ResourceManage")
	public static void deleteLea(long id){
		try{
			LeaMesService.delete(id);
			flash.success("删除成功 ！");
		}catch(Exception e){
			e.printStackTrace();
			flash.success("删除失败 ！");
		}
		list(1,10);
}
	@Check("ResourceManage")
	public static void blank(){
		 
		render();
	}
	
	/**
	 * 前台...添加留言
	 * @param memId
	 * @param lemTitle
	 * @param lemDet
	 */
	@Check("ResourceManage")
	public static void createLea(String memId,String lemTitle,String lemDet){ //后台...数据添加用
		LeaMes lea = new LeaMes();
		lea.member = Member.findById(Long.parseLong(session.get("userid"))); //会员的id 拿到前台登陆会员的名字
		lea.lemTitle = lemTitle;
		lea.lemDet = lemDet;
		lea.lemTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(
						new Date(System.currentTimeMillis())).toString(); //new  SimpleDateFormat ("yyyyMMddd hh:mm:ss").format(new Date(System.currentTimeMillis())).toString();
		LeaMesService.create(lea);
		list(1,10);
	}
	
	/**
	 * 留言
	 * 审核通过
	 * 
	 */
	
	public  static void passlea(String leaid){
		int s = 0 ;
		try{
			LeaMes lea = LeaMes.findById(Long.parseLong(leaid));
			lea .state = 1 ;
			lea.save() ;
			s = 1 ;
			render(s);
		}catch(Exception e){
			s = 0 ;
			e.printStackTrace();
			render(s);
		}
	}
}
